package com.dayuanit.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayuanit.dao.AddressDao;
import com.dayuanit.domain.Address;
import com.dayuanit.exception.MallBusException;
import com.dayuanit.mallenum.AddressDefualtEnum;
import com.dayuanit.mallenum.AddressStatusEnum;
import com.dayuanit.service.AddressService;
import com.dayuanit.service.JedisService;
import com.dayuanit.util.JedisUtil;

@Service
public class AddressServiceImpl implements AddressService {
	
	private static final Logger logger = LoggerFactory.getLogger(JedisServiceImpl.class);
	
	@Autowired
	private AddressDao addressDao;
	
	@Autowired
	private JedisService jedisService;

	@Override
	public List<Map<String, String>> listProvince() {
		
		//先到缓存里找 找得到 直接返回 反之去DB查
		List<Map<String, String>> proList = jedisService.listProvince();
		if (!proList.isEmpty()) {
			logger.info("从缓存获取的省份信息");
			return proList;
		}
		
		logger.info("从DB获取的省份信息");
		proList = addressDao.listProvince();
		
		//放到缓存里面
		jedisService.saveProvince(proList);
		
		return proList;
	}

	@Override
	public List<Map<String, String>> listCity(String provinceCode) {
		//先到缓存里找 找得到 直接返回 反之去DB查
		List<Map<String, String>> cityList = jedisService.listCity(provinceCode);
		if (!cityList.isEmpty()) {
			logger.info("从缓存获取的城市信息");
			return cityList;
		}
		
		logger.info("从DB获取的城市息");
		cityList = addressDao.listCity(provinceCode);
		
		//放到缓存里面
		jedisService.saveCity(provinceCode, cityList);
				
		return cityList;
	}

	@Override
	public List<Map<String, String>> listArea(String cityCode) {
		return addressDao.listArea(cityCode);
	}

	@Override
	public Address saveAddress(Address address) {
		
		//如果当前新增地址为默认，则将数据库默认地址改为非默认
		if (address.getDefaultFlag() == AddressDefualtEnum.ADD_DEFAULT.getFlag()) {
			Address defaultAddress = addressDao.getAddressByDefaultFlag(address.getUserId(), AddressDefualtEnum.ADD_DEFAULT.getFlag());
			if (null != defaultAddress) {
				int rows = addressDao.modifyDefault(defaultAddress.getId(), AddressDefualtEnum.ADD_NON_DEFAULT.getFlag());
				if (rows != 1) {
					throw new MallBusException("增加地址失败");
				}
			}
		}
		
		//TODO 验证用户地址个数 超过最大值不予新增 
		
		//查询省市区的名称
		Map<String, String> proMap = addressDao.getProvince(address.getProvinceCode());
		Map<String, String> cityMap = addressDao.getCity(address.getCityCode());
		Map<String, String> areaMap = addressDao.getArea(address.getAreaCode());
		
		if (null == proMap || null == cityMap || null == areaMap) {
			throw new MallBusException("地址不正确");
		}
		
		address.setProvinceName(proMap.get("name"));
		address.setCityName(cityMap.get("name"));
		address.setAreaName(areaMap.get("name"));
		
		address.setStatus(AddressStatusEnum.NORMAL.getStatus());
		
		addressDao.saveAddress(address);
		return address;
	}

	@Override
	public List<Address> listEnableAddress(Integer userId) {
		return addressDao.listAddress(userId, AddressStatusEnum.NORMAL.getStatus());
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void modifyAddress(Address address, int userId) {
		Address oldAddress = addressDao.getAddressById(address.getId());
		if (null == oldAddress) {
			throw new MallBusException("地址不存在");
		}
		
		if (userId != oldAddress.getUserId().intValue()) {
			throw new MallBusException("地址不属于你");
		}
		
		Map<String, String> map = addressDao.getProvince(address.getProvinceCode());
		if (null == map || map.size() == 0) {
			throw new MallBusException("非法省份");
		}
		
		address.setProvinceName(map.get("name"));
		
		map = addressDao.getCity(address.getCityCode());
		if (null == map || map.size() == 0) {
			throw new MallBusException("非法城市");
		}
		
		address.setCityName(map.get("name"));
		
		map = addressDao.getArea(address.getAreaCode());
		if (null == map || map.size() == 0) {
			throw new MallBusException("非法地区");
		}
		
		address.setAreaName(map.get("name"));
		
		int rows = -1;

		
		if (AddressDefualtEnum.ADD_DEFAULT == AddressDefualtEnum.getAddDefulat(address.getDefaultFlag())) {
			Address defAddress = addressDao.getAddressByDefaultFlag(userId, 1);
			if (null != defAddress) {
				rows = addressDao.modifyDefault(defAddress.getId(), 0);
				if (1 != rows) {
					throw new MallBusException("修改地址失败");
				}
			}
		}
		
		rows = addressDao.updateAddressById(address);
		
		if (1 != rows) {
			throw new MallBusException("修改地址失败");
		}
		
	}

}
